package video

import (
	"github.com/labstack/echo"
	resp "tacitus/library/response"
	"time"
)

func ShowCategoryListHandler(c echo.Context) error {
	i := new(struct{
		ParentID int64 `json:"parent_id"`
	})
	if err := c.Bind(i); err != nil {
		return err
	}
	data, err := SelectCategoryNodeByParent(i.ParentID)
	if err != nil {
		return err
	}
	return resp.Data(c, data)
}

func ShowCategoryTreeHandler(c echo.Context) error {
	i := new(struct{
		RootID int64 `json:"root_id"`
	})
	if err := c.Bind(i); err != nil {
		return err
	}
	res, err := ShowCategoryNodeTree(i.RootID)
	if err != nil {
		return err
	}
	return resp.Data(c, res)
}

func CreateCategoryHandler(c echo.Context) error {
	i := new(struct{
		Category
	})
	if err := c.Bind(i); err != nil {
		return err
	}
	now := time.Now().Unix()
	i.ID = 0
	i.CreateTime = now
	if _, err := rx.Insert(&i.Category); err != nil {
		return err
	}
	return nil
}

func DeleteCategoryHandler(c echo.Context) error {
	i := new(struct{
		ID int64 `json:"id"`
	})
	if err := c.Bind(i); err != nil {
		return err
	}
	if _, err := rx.Delete(&Category{ID: i.ID}); err != nil {
		return err
	}
	return nil
}